package com.ucap.weking.guangan;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.ucap.weking.guangan.Activitys.NewContentActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by weking on 2017/11/24.
 */

public class PushReceiver extends BroadcastReceiver {
    private static final String TAG = "PushReceiver";

    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.i(TAG, "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.i(TAG, "接受到推送下来的自定义消息");

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.i(TAG, "接受到推送下来的通知");
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            String message = bundle.getString(JPushInterface.EXTRA_ALERT);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (!TextUtils.isEmpty(extras)) {
                try {
                    Gson gson = new Gson();
                    ExtrasBean extrasBean = gson.fromJson(extras, ExtrasBean.class);
                    String url = extrasBean.getUrl();
                    NewContentActivity.startThisActivity(context, title, url);
                } catch (Exception e) {
                }
            }
            Log.i(TAG, "用户点击打开了通知  title" + title + "  message " + message + "  extras" + extras);

        } else {
            Log.i(TAG, "Unhandled intent - " + intent.getAction());
        }
    }

    class ExtrasBean {

        /**
         * url : www.guang-an.gov.cn
         */

        private String url;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }

}
